package com.liwen.service.depend;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.liwen.dao.entity.OrderEntity;
import com.liwen.service.BaseSupport;
import com.liwen.service.file.Consumer;
import com.liwen.service.file.FileProducer;
import com.liwen.service.file.Storage;
import com.liwen.service.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class FileService extends BaseSupport {

    private static Logger log = LoggerFactory.getLogger(FileService.class);

    @Resource
    private TaskExecutor taskExecutor;

    @Resource
    private OrderService orderService;

    public void pullRepayPlan(String date) {

        byte[] loanBytes = FileUtil.FileToByte(new File("D:/data/test.txt"));
        final CountDownLatch latch = new CountDownLatch(3);
        final ConcurrentHashMap map = new ConcurrentHashMap();
        try {
            final FileProducer producer = new FileProducer(loanBytes, 1, 240);
            Consumer consumer = new Consumer<List<String>>(new Storage(producer)) {
                @Override
                public void consume(List<String> product) {
                    dealFile(product, map);
                }

                @Override
                protected void afterCompleted() {
                    latch.countDown();
                }
            };
            //启动三个线程去执行任务
            for (int i = 0; i < 3; i++) {
//                taskExecutor.execute(consumer); 或者如下写法
                run(consumer);
            }
            latch.await(3, TimeUnit.HOURS); // 设置3h完成时间 超时则不再进行
            if (latch.getCount() > 0) {
                log.error("pullRepayPlan-readRepay-sum=date:{},msg:{}", date);
                return;
            }
        } catch (Exception e) {
            log.error("pullRepayPlan-readRepay-sum=date:{},msg:{}", date, e.getMessage());
        }
    }


    private void dealFile(List<String> product, ConcurrentHashMap map) {

    }

    public String execute() {
        int consumer = 3;
        List<OrderEntity> list = new ArrayList<>();
        list.add(orderService.bysn(11, "dadsad"));
        if (CollectionUtils.isEmpty(list))
            return "无可执行任务";
        int total = list.size();
        final Queue<OrderEntity> queue = new LinkedBlockingQueue<>(list);
        final CountDownLatch latch = new CountDownLatch(consumer);
        final AtomicInteger fail = new AtomicInteger(0);
        for (int i = 0; i < consumer; i++) {
            run(new Runnable() {
                @Override
                public void run() {
                    OrderEntity task = null;
                    while ((task = queue.poll()) != null) {
                        log.info("retry=ins:{},sn:{}", task.getInsId(), task.getSn());
                    }
                    latch.countDown();
                }
            });
        }
        try {
            latch.await();
        } catch (InterruptedException e) {

        }
        return "total:" + total + ".fail:" + fail.get();
    }

}
